package com.cwvs.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.cwvs.utils.MySqlPageUtil;
import com.cwvs.utils.Pages;

/**
 * 支付相关dao
 * 
 */
@Repository
public class AppPayDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private MySqlPageUtil mySqlPageUtil;

	public List<String> getPay(String orderType){
		if(StringUtils.isBlank(orderType)){
			return new ArrayList<String>();
		}
		return jdbcTemplate.query("SELECT PAY_ID FROM APP_ORDER_PAY WHERE ORDER_TYPE=? AND IS_ACTIVE=1 ",new Object[]{orderType},new RowMapper<String>(){
			@Override
			public String mapRow(ResultSet r, int i) throws SQLException {
				return r.getString("PAY_ID");
			}
		});
	}
	
	/**
	 * 支付方式配置列表
	 * 
	 * @return
	 */
	public Pages payList(Map<String, Object> m, Pages page) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT T1.ID,T1.ORDER_TYPE,T1.PAY_ID,T1.IS_ACTIVE ");
		sql.append(" ,T2.NAME TYPE_NAME,T2.REMARK,T3.NAME PAY_NAME,T3.DESCRIPTION PAY_DESCRIPTION ");
		sql.append(" FROM APP_ORDER_PAY T1 ");
		sql.append(" LEFT JOIN APP_ORDER_TYPE T2 ON T1.ORDER_TYPE=T2.ID ");
		sql.append(" LEFT JOIN APP_PAY T3 ON T1.PAY_ID=T3.ID ");
		List<Object> params = new ArrayList<Object>();
		String page_sql = mySqlPageUtil.getPageSql(page, sql.toString());
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(page_sql,
				params.toArray());
		page.setPage(ls);
		page.setTotalRecord(payCount(m));
		return page;
	}

	private int payCount(Map<String, Object> m) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(1) FROM APP_ORDER_PAY T1 WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		return jdbcTemplate.queryForInt(sql.toString(), params.toArray());
	}
	/**
	 * 获取所有的支付类型
	 * @return
	 */
	public List<Map<String,Object>> getAllPay(){
		StringBuffer sbf=new StringBuffer();
		sbf.append("SELECT ID,NAME,DESCRIPTION FROM APP_PAY ");
		return jdbcTemplate.queryForList(sbf.toString());
	}
	
	/**
	 * 判断对应的支付方式是否存在
	 */
	public boolean isExists(String orderTtype,String payId){
		if(StringUtils.isBlank(orderTtype)||StringUtils.isBlank(payId)){
			return true;
		}
		return jdbcTemplate.queryForInt("SELECT COUNT(1) FROM APP_ORDER_PAY WHERE ORDER_TYPE=? AND PAY_ID=?",new Object[]{orderTtype,payId})>0;
	}
	/**
	 * 保存订单支付方式
	 */
	public boolean savePay(String orderTtype,String payId){
		if(StringUtils.isBlank(orderTtype)||StringUtils.isBlank(payId)){
			return false;
		}
		StringBuffer sbf=new StringBuffer();
		sbf.append(" INSERT INTO APP_ORDER_PAY(ID,ORDER_TYPE,PAY_ID,IS_ACTIVE)");
		sbf.append("VALUES(?,?,?,?)");
		String id=UUID.randomUUID().toString();
		return jdbcTemplate.update(sbf.toString(),new Object[]{id,orderTtype,payId,1})>0;
	}
	/**
	 * 更新支付方式状态
	 */
	public boolean updateStatus(String id,boolean status){
		return jdbcTemplate.update("UPDATE APP_ORDER_PAY SET IS_ACTIVE=? WHERE ID=? ",new Object[]{status,id})>0;
	}
}
